/**
 * 定义分页全局变量
 */
var page_ = new page({
	search : search
});
/**
 * 定义容器全局变量
 */
var con = new container({
	datagrid : '#datagrid',
	datagridtoolbar : '#datagrid-toolbar',
	singleSelect : true
});
/**
 * 定义标记全局变量
 */
var flag=0;
/**
 * 初始化发件人
 */
init_user();

$(function() {
	/**
	 * 查询按钮设置
	 */
	$("#datagrid-toolbar #search").linkbutton({
		onClick : function() {
			search(page_);
		}
	});
	/**
	 * 转发按钮设置
	 */
	$("#datagrid-toolbar #re_message").linkbutton({
		onClick : function() {
			reMessage();
		}
	});
	/**
	 * 删除按钮设置
	 */
	$("#datagrid-toolbar #del_message").linkbutton({
		onClick : function() {
			delMessage();
		}
	});
});
/**
 * add.jsp初始化发件人
 */
function init_user() {
	var userId = $('#userId').val();
	var userName = $('#userName').val();
	$('#senduser').combobox({
		valueField : 'value',
		textField : 'text',
		data : [ {
			value : userId,
			text : userName
		} ],
		editable : false,
		onLoadSuccess : function() {
			$("#senduser").combobox("select", userId);
			$("#addressee").combobox({});			
			search(page_);
		}
	});
}

/**
 * 获取查询条件 * 
 * @returns
 */
function getCondition() {
	var condition = {};
	condition.userName = $("#senduser").combobox('getText');
	condition.addresseeName = $("#addressee").combobox('getText');
	return condition;
}
/**
 * 查询
 */
function search(pg) {
	var url = ctx + "/sendMessage/query";
	page_.pageSize = pg.pageSize;
	page_.pageNumber = pg.pageNumber;
	query(url, getCondition(), con, page_);
}

/**
 * 转发
 */
function reMessage() {
	var reMessage = getSelectedRow("#datagrid");
	if (!reMessage) {
		return;
	}
	$('#dialog').dialog({
		title : '转发',
		width : 820,
		height : 470,
		left : 100,
		top : 20,
		href : 'message/readd.jsp',
		closed : false,
		cache : false,
		modal : true,
	    onClose:function(){
	    	destroy('dialog','#dialogDiv');
	    },
		onLoad : function() {
			/**
			 * 添加收件人按钮设置
			 */
			$("#addAddressee").linkbutton({
				iconCls : 'icon-add',
				plain : true,
				onClick : function() {
					addAddressee();
				}
			});
			/**
			 * 移除收件人按钮设置
			 */
			$("#delAddressee").linkbutton({
				iconCls : 'icon-remove',
				plain : true,
				onClick : function() {
					delAddressee();
				}
			});
			$("#title2").textbox("setValue",reMessage.title);
			$("#title2").textbox({ editable : false});
			$("#contents2").textbox("setValue",reMessage.content);
			init_AddresseeType();
		},
		buttons : [ {
			text : '保存',
			iconCls : 'icon-ok',
			handler : function() {
				add(reMessage);
				if(flag==1){
					message("收件人不能为空");
					return;
				}
			}
		}, {
			text : '取消',
			iconCls : 'icon-cancel',
			handler : function() {
				$('#dialog').dialog('close');
			}
		} ]
	});
};

/**
 * 删除信息
 */
function delMessage() {
	var row = getSelectedRow("#datagrid");
	if (!row) {
		return;
	}
	$.messager.confirm('提示', '此操作将同时删除收件方此信息,确定删除?', function(r) {
		if (r) {
			$.ajax({
				type : "POST",
				url : ctx + "/sendMessage/delete",
				dataType : "json",
				data : {
					id : row.sendMessageId
				},
				success : function(data) {
					if(!showMsg(data,"删除成功")){
						return;
					}
					search(page_);
				},
			});
		}
	})
}
/**
 * 转发信息
 */
function add(reMessage) {
	flag=0;
	var getRows = $("#addGrid").datagrid("getRows");
	var addresseeIds = new Array();
	var addresseeNames = new Array();
	var codes = new Array();
	var addresseeTypeNames = new Array();
	$.each(getRows, function(index, row) {
		addresseeIds.push(row.addresseeId);
		addresseeNames.push(row.addresseeName);
		codes.push(row.code);
		addresseeTypeNames.push(row.addresseeTypeName);
	});
	if(reMessage!=null){
		var sendMessageId=reMessage.sendMessageId
	}
	if($.trim(addresseeIds).length=='0'){
		flag=1;
		return;
	}
	var data = {
		title : $("#title").textbox('getValue'),
		content : $("#contents").textbox('getValue'),
		userId : $("#adduser").combobox('getValue'),
		userName : $("#adduser").combobox('getText'),
		remind : $("input[name='remind']:checked").val(),
		addresseeIds : addresseeIds.join(","),
		addresseeNames : addresseeNames.join(","),
		codes : codes.join(","),
		addresseeTypeNames : addresseeTypeNames.join(","),
		sendMessageId : sendMessageId
	};
	$.ajax({
		type : "POST",
		url : ctx + "/receiveMessage/writeMessage",
		dataType : "json",
		data : data,
		success : function(data) {
			if(!showMsg(data,"发送成功")){
				return;
			}
			$('#dialog').dialog('close');
			search(page_);
		},
	});
}